package middleware

import (
	"log/slog"
	"time"

	"github.com/gin-gonic/gin"
)

func LoggingMiddleware() gin.HandlerFunc {
	return func(c *gin.Context) {
		start := time.Now()

		// 处理请求前记录
		slog.Info("request started",
			"method", c.Request.Method,
			"path", c.Request.URL.Path,
			"client_ip", c.ClientIP(),
		)

		c.Next() // 处理请求

		// 请求处理后记录
		duration := time.Since(start)
		slog.Info("request completed",
			"status", c.Writer.Status(),
			"duration_ms", duration.Milliseconds(),
			"errors", c.Errors.String(),
		)
	}
}
